package br.com.toscano.contpedido.view;

import java.util.Iterator;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import br.com.toscano.contpedido.business.PedidoBC;
import br.com.toscano.contpedido.otd.ProducaoOTD;
import br.gov.frameworkdemoiselle.annotation.NextView;
import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractListPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;


@ViewController
@NextView("./pedido_listar_producao.jsf")
@PreviousView("./pedido_listar_producao.jsf")
public class PedidoListarProducaoMB extends AbstractListPageBean<ProducaoOTD, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private PedidoBC pedidoBC;

	private Long LocalProducao;

	public Long getLocalProducao() {
		return LocalProducao;
	}

	public void setLocalProducao(Long LocalProducao) {
		this.LocalProducao = LocalProducao;
	}
	public void filtrar() {
		super.clear();
		super.getDataModel();
	}
	
	@Override
	protected List<ProducaoOTD> handleResultList() {

		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();    
		if (request.getParameter("idLocalProducao") != null && !"".equals(request.getParameter("idLocalProducao")))
			this.setLocalProducao(new Long(request.getParameter("idLocalProducao")));
		
		return pedidoBC.gerarListagemProducao(this.getLocalProducao());
	}

	@Transactional
	public String deletesSelection() {
		boolean delete;
		for (Iterator<Long> iter = getSelection().keySet().iterator(); iter.hasNext();) {
			Long id = iter.next();
			delete = getSelection().get(id);
			if (delete) {
				pedidoBC.registrarProducao(id);
				iter.remove();
			}
		}

		super.clear();
		return getPreviousView();
	}
	
	public void trocarLocalProducao() {
		filtrar();
	}
}